Class StdDraw

java.lang.Object
  extended by StdDraw
All Implemented Interfaces:
java.awt.event.ActionListener, java.awt.event.KeyListener, java.awt.event.MouseListener, java.awt.event.MouseMotionListener, java.util.EventListener

public final class StdDraw
extends java.lang.Object
implements java.awt.event.ActionListener, java.awt.event.MouseListener, java.awt.event.MouseMotionListener, java.awt.event.KeyListener

Standard draw. Our class StdDraw provides a basic capability for creating drawings with your programs. It uses a simple graphics model that allows you to create drawings consisting of points, lines, and curves in a window on your computer and to save the drawings to a file.

For documentation, see Section 1.5 of Introduction to Programming in Java: An Interdisciplinary Approach, Spring 2007 preliminary version and http://www.cs.princeton.edu /introcs/15inout


Field Summary
static java.awt.Color BLACK
           
static java.awt.Color BLUE
           
private static double BORDER
           
static java.awt.Color CYAN
           
static java.awt.Color DARK_GRAY
           
private static java.awt.Color DEFAULT_CLEAR_COLOR
           
private static java.awt.Font DEFAULT_FONT
           
private static java.awt.Color DEFAULT_PEN_COLOR
           
private static double DEFAULT_PEN_RADIUS
           
private static int DEFAULT_SIZE
           
private static double DEFAULT_XMAX
           
private static double DEFAULT_XMIN
           
private static double DEFAULT_YMAX
           
private static double DEFAULT_YMIN
           
private static boolean defer
           
private static java.awt.Font font
           
private static javax.swing.JFrame frame
           
static java.awt.Color GRAY
           
static java.awt.Color GREEN
           
private static int height
           
private static java.util.LinkedList<java.lang.Character> keysTyped
           
static java.awt.Color LIGHT_GRAY
           
static java.awt.Color MAGENTA
           
private static boolean mousePressed
           
private static double mouseX
           
private static double mouseY
           
private static java.awt.Graphics2D offscreen
           
private static java.awt.image.BufferedImage offscreenImage
           
private static java.awt.Graphics2D onscreen
           
private static java.awt.image.BufferedImage onscreenImage
           
static java.awt.Color ORANGE
           
private static java.awt.Color penColor
           
private static double penRadius
           
static java.awt.Color PINK
           
static java.awt.Color RED
           
static StdDraw std
           
static java.awt.Color WHITE
           
private static int width
           
private static double xmax
           
private static double xmin
           
static java.awt.Color YELLOW
           
private static double ymax
           
private static double ymin
           
 
Constructor Summary
StdDraw()
           
 
Method Summary
 void actionPerformed(java.awt.event.ActionEvent e)
          Deprecated. Open a save dialog when the user selects "Save As" from the menu
static void arc(double x, double y, double r, double angle1, double angle2)
          Draw an arc of radius r, centered on (x, y), from angle1 to angle2 (in degrees).
static void circle(double x, double y, double r)
          Draw circle of radius r, centered on (x, y); degenerate to pixel if small
static void clear()
          Clear the screen with the default color, white
static void clear(java.awt.Color color)
          Clear the screen with the given color.
private static javax.swing.JMenuBar createMenuBar()
           
private static double factorX(double w)
           
private static double factorY(double h)
           
static void filledCircle(double x, double y, double r)
          Draw filled circle of radius r, centered on (x, y); degenerate to pixel if small
static void filledPolygon(double[] x, double[] y)
          Draw a filled polygon with the given (x[i], y[i]) coordinates
static void filledSquare(double x, double y, double r)
          Draw a filled square of side length 2r, centered on (x, y); degenerate to pixel if small
static void filledSquare(double x, double y, double r, boolean t)
          modified by Karl Koch by adding the boolean t if t = false the image will not be repainted in order to speed up this method
private static java.awt.Image getImage(java.lang.String filename)
          Drawing images.
static boolean hasNextKeyTyped()
          Has the user typed a key?
private static void init()
           
 void keyPressed(java.awt.event.KeyEvent e)
          Deprecated.  
 void keyReleased(java.awt.event.KeyEvent e)
          Deprecated.  
 void keyTyped(java.awt.event.KeyEvent e)
          Deprecated.  
static void line(double x0, double y0, double x1, double y1)
          Draw a line from (x0, y0) to (x1, y1)
static void line(double x0, double y0, double x1, double y1, boolean t)
          method added by Karl Koch including the boolean
static void main(java.lang.String[] args)
          Deprecated.  
 void mouseClicked(java.awt.event.MouseEvent e)
          Deprecated.  
 void mouseDragged(java.awt.event.MouseEvent e)
          Deprecated.  
 void mouseEntered(java.awt.event.MouseEvent e)
          Deprecated.  
 void mouseExited(java.awt.event.MouseEvent e)
          Deprecated.  
 void mouseMoved(java.awt.event.MouseEvent e)
          Deprecated.  
static boolean mousePressed()
          Is the mouse being pressed?
 void mousePressed(java.awt.event.MouseEvent e)
          Deprecated.  
 void mouseReleased(java.awt.event.MouseEvent e)
          Deprecated.  
static double mouseX()
          Where is the mouse?
static double mouseY()
          Where is the mouse?
static char nextKeyTyped()
          What is the next key that was typed by the user?
static void picture(double x, double y, java.lang.String s)
          Draw picture (gif, jpg, or png) centered on (x, y).
static void picture(double x, double y, java.lang.String s, double degrees)
          Draw picture (gif, jpg, or png) centered on (x, y), rotated given number of degrees
static void picture(double x, double y, java.lang.String s, double w, double h)
          Draw picture (gif, jpg, or png) centered on (x, y).
static void picture(double x, double y, java.lang.String s, double w, double h, double degrees)
          Draw picture (gif, jpg, or png) centered on (x, y), rotated given number of degrees, rescaled to w-by-h.
static void pixel(double x, double y)
          Draw one pixel at (x, y)
static void point(double x, double y)
          Draw a point at (x, y)
static void polygon(double[] x, double[] y)
          Draw a polygon with the given (x[i], y[i]) coordinates
static void save(java.lang.String filename)
          Save to file - suffix must be png, jpg, or gif.
private static double scaleX(double x)
           
private static double scaleY(double y)
           
static void setCanvasSize(int w, int h)
          Set the window size to w-by-h pixels
static void setFont()
          Set the font to be the default for all string writing
static void setFont(java.awt.Font f)
          Set the font as given for all string writing
static void setPenColor()
          Set the pen color to the default which is BLACK.
static void setPenColor(java.awt.Color color)
          Set the pen color to the given color.
static void setPenRadius()
          Set the pen size to the default
static void setPenRadius(double r)
          Set the pen size to the given size
static void setXscale()
          Set the X scale to be the default
static void setXscale(double min, double max)
          Set the X scale (a border is added to the values)
static void setYscale()
          Set the Y scale to be the default
static void setYscale(double min, double max)
          Set the Y scale (a border is added to the values)
static void show()
          Display on-screen; calling this method means that the screen WILL be redrawn after each line(), circle(), or square().
static void show(int t)
          Display on screen and pause for t milliseconds.
static void square(double x, double y, double r)
          Draw squared of side length 2r, centered on (x, y); degenerate to pixel if small
static void text(double x, double y, java.lang.String s)
          Write the given text string in the current font, center on (x, y).
private static double userX(double x)
           
private static double userY(double y)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

BLACK

public static final java.awt.Color BLACK

BLUE

public static final java.awt.Color BLUE

CYAN

public static final java.awt.Color CYAN

DARK_GRAY

public static final java.awt.Color DARK_GRAY

GRAY

public static final java.awt.Color GRAY

GREEN

public static final java.awt.Color GREEN

LIGHT_GRAY

public static final java.awt.Color LIGHT_GRAY

MAGENTA

public static final java.awt.Color MAGENTA

ORANGE

public static final java.awt.Color ORANGE

PINK

public static final java.awt.Color PINK

RED

public static final java.awt.Color RED

WHITE

public static final java.awt.Color WHITE

YELLOW

public static final java.awt.Color YELLOW

DEFAULT_PEN_COLOR

private static final java.awt.Color DEFAULT_PEN_COLOR

DEFAULT_CLEAR_COLOR

private static final java.awt.Color DEFAULT_CLEAR_COLOR

penColor

private static java.awt.Color penColor

DEFAULT_SIZE

private static final int DEFAULT_SIZE
See Also:
Constant Field Values

width

private static int width

height

private static int height

DEFAULT_PEN_RADIUS

private static final double DEFAULT_PEN_RADIUS
See Also:
Constant Field Values

penRadius

private static double penRadius

defer

private static boolean defer

BORDER

private static final double BORDER
See Also:
Constant Field Values

DEFAULT_XMIN

private static final double DEFAULT_XMIN
See Also:
Constant Field Values

DEFAULT_XMAX

private static final double DEFAULT_XMAX
See Also:
Constant Field Values

DEFAULT_YMIN

private static final double DEFAULT_YMIN
See Also:
Constant Field Values

DEFAULT_YMAX

private static final double DEFAULT_YMAX
See Also:
Constant Field Values

xmin

private static double xmin

ymin

private static double ymin

xmax

private static double xmax

ymax

private static double ymax

DEFAULT_FONT

private static final java.awt.Font DEFAULT_FONT

font

private static java.awt.Font font

offscreenImage

private static java.awt.image.BufferedImage offscreenImage

onscreenImage

private static java.awt.image.BufferedImage onscreenImage

offscreen

private static java.awt.Graphics2D offscreen

onscreen

private static java.awt.Graphics2D onscreen

std

public static StdDraw std

frame

private static javax.swing.JFrame frame

mousePressed

private static boolean mousePressed

mouseX

private static double mouseX

mouseY

private static double mouseY

keysTyped

private static java.util.LinkedList<java.lang.Character> keysTyped
Constructor Detail

StdDraw

public StdDraw()
Method Detail

setCanvasSize

public static void setCanvasSize(int w,
                                 int h)
Set the window size to w-by-h pixels

Parameters:
w - the width as a number of pixels
h - the height as a number of pixels
Throws:
a - RunTimeException if the width or height is 0 or negative.

init

private static void init()

createMenuBar

private static javax.swing.JMenuBar createMenuBar()

setXscale

public static void setXscale()
Set the X scale to be the default


setYscale

public static void setYscale()
Set the Y scale to be the default


setXscale

public static void setXscale(double min,
                             double max)
Set the X scale (a border is added to the values)

Parameters:
min - the minimum value of the X scale
max - the maximum value of the X scale

setYscale

public static void setYscale(double min,
                             double max)
Set the Y scale (a border is added to the values)

Parameters:
min - the minimum value of the Y scale
max - the maximum value of the Y scale

scaleX

private static double scaleX(double x)

scaleY

private static double scaleY(double y)

factorX

private static double factorX(double w)

factorY

private static double factorY(double h)

userX

private static double userX(double x)

userY

private static double userY(double y)

clear

public static void clear()
Clear the screen with the default color, white


clear

public static void clear(java.awt.Color color)
Clear the screen with the given color.

Parameters:
color - the Color to make the background

setPenRadius

public static void setPenRadius()
Set the pen size to the default


setPenRadius

public static void setPenRadius(double r)
Set the pen size to the given size

Parameters:
r - the radius of the pen
Throws:
java.lang.RuntimeException - if r is negative

setPenColor

public static void setPenColor()
Set the pen color to the default which is BLACK.


setPenColor

public static void setPenColor(java.awt.Color color)
Set the pen color to the given color. The available pen colors are BLACK, BLUE, CYAN, DARK_GRAY, GRAY, GREEN, LIGHT_GRAY, MAGENTA, ORANGE, PINK, RED, WHITE, and YELLOW.

Parameters:
color - the Color to make the pen

setFont

public static void setFont()
Set the font to be the default for all string writing


setFont

public static void setFont(java.awt.Font f)
Set the font as given for all string writing

Parameters:
f - the font to make text

line

public static void line(double x0,
                        double y0,
                        double x1,
                        double y1)
Draw a line from (x0, y0) to (x1, y1)

Parameters:
x0 - the x-coordinate of the starting point
y0 - the y-coordinate of the starting point
x1 - the x-coordinate of the destination point
y1 - the y-coordinate of the destination point

line

public static void line(double x0,
                        double y0,
                        double x1,
                        double y1,
                        boolean t)
method added by Karl Koch including the boolean

Parameters:
x0 -
y0 -
x1 -
y1 -
t -

pixel

public static void pixel(double x,
                         double y)
Draw one pixel at (x, y)

Parameters:
x - the x-coordinate of the pixel
y - the y-coordinate of the pixel

point

public static void point(double x,
                         double y)
Draw a point at (x, y)

Parameters:
x - the x-coordinate of the point
y - the y-coordinate of the point

circle

public static void circle(double x,
                          double y,
                          double r)
Draw circle of radius r, centered on (x, y); degenerate to pixel if small

Parameters:
x - the x-coordinate of the center of the circle
y - the y-coordinate of the center of the circle
r - the radius of the circle
Throws:
java.lang.RuntimeException - if the radius of the circle is negative

filledCircle

public static void filledCircle(double x,
                                double y,
                                double r)
Draw filled circle of radius r, centered on (x, y); degenerate to pixel if small

Parameters:
x - the x-coordinate of the center of the circle
y - the y-coordinate of the center of the circle
r - the radius of the circle
Throws:
java.lang.RuntimeException - if the radius of the circle is negative

arc

public static void arc(double x,
                       double y,
                       double r,
                       double angle1,
                       double angle2)
Draw an arc of radius r, centered on (x, y), from angle1 to angle2 (in degrees).

Parameters:
x - the x-coordinate of the center of the circle
y - the y-coordinate of the center of the circle
r - the radius of the circle
angle1 - the starting angle. 0 would mean an arc beginning at 3 o'clock.
angle2 - the angle at the end of the arc. For example, if you want a 90 degree arc, then angle2 should be angle1 + 90.
Throws:
java.lang.RuntimeException - if the radius of the circle is negative

square

public static void square(double x,
                          double y,
                          double r)
Draw squared of side length 2r, centered on (x, y); degenerate to pixel if small

Parameters:
x - the x-coordinate of the center of the square
y - the y-coordinate of the center of the square
r - radius is half the length of any side of the square
Throws:
java.lang.RuntimeException - if r is negative

filledSquare

public static void filledSquare(double x,
                                double y,
                                double r)
Draw a filled square of side length 2r, centered on (x, y); degenerate to pixel if small

Parameters:
x - the x-coordinate of the center of the square
y - the y-coordinate of the center of the square
r - radius is half the length of any side of the square
Throws:
java.lang.RuntimeException - if r is negative

filledSquare

public static void filledSquare(double x,
                                double y,
                                double r,
                                boolean t)
modified by Karl Koch by adding the boolean t if t = false the image will not be repainted in order to speed up this method

Parameters:
x -
y -
r -
t -

polygon

public static void polygon(double[] x,
                           double[] y)
Draw a polygon with the given (x[i], y[i]) coordinates

Parameters:
x - an array of all the x-coordindates of the polygon
y - an array of all the y-coordindates of the polygon

filledPolygon

public static void filledPolygon(double[] x,
                                 double[] y)
Draw a filled polygon with the given (x[i], y[i]) coordinates

Parameters:
x - an array of all the x-coordindates of the polygon
y - an array of all the y-coordindates of the polygon

getImage

private static java.awt.Image getImage(java.lang.String filename)
Drawing images.


picture

public static void picture(double x,
                           double y,
                           java.lang.String s)
Draw picture (gif, jpg, or png) centered on (x, y).

Parameters:
x - the center x-coordinate of the image
y - the center y-coordinate of the image
s - the name of the image/picture, e.g., "ball.gif"
Throws:
java.lang.RuntimeException - if the image's width or height are negative

picture

public static void picture(double x,
                           double y,
                           java.lang.String s,
                           double degrees)
Draw picture (gif, jpg, or png) centered on (x, y), rotated given number of degrees

Parameters:
x - the center x-coordinate of the image
y - the center y-coordinate of the image
s - the name of the image/picture, e.g., "ball.gif"
degrees - is the number of degrees to rotate counterclockwise
Throws:
java.lang.RuntimeException - if the image's width or height are negative

picture

public static void picture(double x,
                           double y,
                           java.lang.String s,
                           double w,
                           double h)
Draw picture (gif, jpg, or png) centered on (x, y). Rescaled to w-by-h.

Parameters:
x - the center x coordinate of the image
y - the center y coordinate of the image
s - the name of the image/picture, e.g., "ball.gif"
w - the width of the image
h - the height of the image

picture

public static void picture(double x,
                           double y,
                           java.lang.String s,
                           double w,
                           double h,
                           double degrees)
Draw picture (gif, jpg, or png) centered on (x, y), rotated given number of degrees, rescaled to w-by-h.

Parameters:
x - the center x-coordinate of the image
y - the center y-coordinate of the image
s - the name of the image/picture, e.g., "ball.gif"
w - the width of the image
h - the height of the image
degrees - is the number of degrees to rotate counterclockwise
Throws:
java.lang.RuntimeException - if the image's width or height are negative

text

public static void text(double x,
                        double y,
                        java.lang.String s)
Write the given text string in the current font, center on (x, y).

Parameters:
x - the center x coordinate of the text
y - the center y coordinate of the text
s - the text

show

public static void show(int t)
Display on screen and pause for t milliseconds. Calling this method means that the screen will NOT be redrawn after each line(), circle(), or square(). This is useful when there are many methods to call to draw a complete picture.

Parameters:
t - number of milliseconds

show

public static void show()
Display on-screen; calling this method means that the screen WILL be redrawn after each line(), circle(), or square(). This is the default.


save

public static void save(java.lang.String filename)
Save to file - suffix must be png, jpg, or gif.

Parameters:
filename - the name of the file with one of the required suffixes

actionPerformed

public void actionPerformed(java.awt.event.ActionEvent e)
Deprecated. Open a save dialog when the user selects "Save As" from the menu

Specified by:
actionPerformed in interface java.awt.event.ActionListener

mousePressed

public static boolean mousePressed()
Is the mouse being pressed?

Returns:
true or false

mouseX

public static double mouseX()
Where is the mouse?

Returns:
the value of the x-coordinate of the mouse

mouseY

public static double mouseY()
Where is the mouse?

Returns:
the value of the y-coordinate of the mouse

mouseClicked

public void mouseClicked(java.awt.event.MouseEvent e)
Deprecated. 

Specified by:
mouseClicked in interface java.awt.event.MouseListener

mouseEntered

public void mouseEntered(java.awt.event.MouseEvent e)
Deprecated. 

Specified by:
mouseEntered in interface java.awt.event.MouseListener

mouseExited

public void mouseExited(java.awt.event.MouseEvent e)
Deprecated. 

Specified by:
mouseExited in interface java.awt.event.MouseListener

mousePressed

public void mousePressed(java.awt.event.MouseEvent e)
Deprecated. 

Specified by:
mousePressed in interface java.awt.event.MouseListener

mouseReleased

public void mouseReleased(java.awt.event.MouseEvent e)
Deprecated. 

Specified by:
mouseReleased in interface java.awt.event.MouseListener

mouseDragged

public void mouseDragged(java.awt.event.MouseEvent e)
Deprecated. 

Specified by:
mouseDragged in interface java.awt.event.MouseMotionListener

mouseMoved

public void mouseMoved(java.awt.event.MouseEvent e)
Deprecated. 

Specified by:
mouseMoved in interface java.awt.event.MouseMotionListener

hasNextKeyTyped

public static boolean hasNextKeyTyped()
Has the user typed a key?

Returns:
true if the user has typed a key, false otherwise

nextKeyTyped

public static char nextKeyTyped()
What is the next key that was typed by the user?

Returns:
the next key typed

keyTyped

public void keyTyped(java.awt.event.KeyEvent e)
Deprecated. 

Specified by:
keyTyped in interface java.awt.event.KeyListener

keyPressed

public void keyPressed(java.awt.event.KeyEvent e)
Deprecated. 

Specified by:
keyPressed in interface java.awt.event.KeyListener

keyReleased

public void keyReleased(java.awt.event.KeyEvent e)
Deprecated. 

Specified by:
keyReleased in interface java.awt.event.KeyListener

main

public static void main(java.lang.String[] args)
Deprecated.